package util

import (
	"fmt"
)

type doubleLink struct {
	data map[string]interface{}
	next *doubleLink
}

func NewNode(a, b int) *doubleLink {
	d := make(map[string]interface{})
	d["one"] = a
	d["two"] = b
	var p doubleLink
	p.data = d
	p.next = nil
	return &p
}
func (dl *doubleLink) insertPriorLink(a, b int) {
	p := NewNode(a, b)
	p.next = dl.next
	dl.next = p
}
func (dl *doubleLink) insertEndLink(a, b int) {
	for {
		if dl.next == nil {
			p := NewNode(a, b)
			dl.next = p
			break
		}
		dl = dl.next
	}
}
func (dl *doubleLink) printDoubleLink() {
	var end doubleLink
	end = *dl.next
	for {
		fmt.Println("node data", end.data["one"], end.data["two"])
		if end.next == nil {
			break
		}
		end = *end.next
	}
}
func (dl *doubleLink) insertPositionLink(a, b, n int) {
	i := 1
	for {
		if i == n {
			p := NewNode(a, b)
			p.next = dl.next
			dl.next = p
			break
		}
		if dl.next == nil {
			break
		}

		i++
		dl = dl.next
	}
}
func (dl *doubleLink) deletePositionLink(n int) {
	i := 1
	for {
		if i == n {
			p := *dl.next
			dl.next = p.next
			delete(p.data, "one")
			delete(p.data, "two")

			break
		}
		if dl.next == nil {
			break
		}

		i++
		dl = dl.next
	}
}
func Test() {
	fmt.Println("main start")
	var head doubleLink
	head.next = nil
	head.insertPriorLink(1, 2)
	head.insertPriorLink(3, 4)
	head.insertPriorLink(5, 6)
	head.printDoubleLink()

	head.insertEndLink(7, 8)
	head.insertEndLink(9, 10)
	head.insertEndLink(11, 12)
	head.insertPositionLink(0, 0, 3)
	head.printDoubleLink()

	head.deletePositionLink(2)
	head.printDoubleLink()
}
